#!/usr/bin/env python
# -*- coding: UTF-8 -*-
"""
@Project ：turbo 
@File    ：handlers.py
@Author  ：赵加响
@Date    ：2023/9/7 10:04 
@Desc    ：
"""
import functools
import logging
import traceback
from datetime import datetime

logger = logging.getLogger('django.debug')


def except_catch(func):
    """
    通过装饰器进行异常捕获处理
    主要用于线上调试
    导入方式：from utils.decorators.handlers import except_catch
    :param func:
    :return:
    """

    @functools.wraps(func)
    def wrapper(*args, **kwargs):
        try:
            return func(*args, **kwargs)
        except Exception as e:
            sign = '=' * 60 + '\n'
            exception_info = f'\n{sign}>>>异常时间：\t{datetime.now()}\n>>>异常函数：\t{func.__name__}\n>>>异常信息：\t{e}'
            trace_back = f'\n{sign}{traceback.format_exc()}{sign}'

            logger.debug(exception_info)
            logger.debug(trace_back)

            raise e

    return wrapper
